#include <my_header.h>



int main(int argc,char* argv[])
{
    //./client 192.168.8.129 8080
    ARGS_CHECK(argc,3);
    char *ip = argv[1];
    char *port=argv[2];

    int socket_fd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socket_fd,-1,"socket");
    struct sockaddr_in sockaddr;
    sockaddr.sin_family=AF_INET;
    sockaddr.sin_port=htons(atoi(port));
    sockaddr.sin_addr.s_addr=inet_addr(ip);

    connect(socket_fd,(struct sockaddr *)&sockaddr,sizeof(sockaddr));
    fd_set set;
    char buf[1024]={0};
    char buf2[100]={0};
    while(1){
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO,&set);
        FD_SET(socket_fd,&set);
        select(100,&set,NULL,NULL,NULL);
        //客户输入指令
        if(FD_ISSET(STDIN_FILENO,&set)){
            bzero(buf,sizeof(buf));
            int ret=read(STDIN_FILENO,buf,sizeof(buf));
            memcpy(buf2,buf,sizeof(buf2));
            if(ret==0){
                break;
            }
            //分析指令
            //TODO
            //ch存储指令
            char *ch=strtok(buf," ");
            char ch1[100]={0};
            strncpy(ch1,ch,strlen(ch)-1);
            
            //printf("指令是：%s\n",ch);
            //p存储路径
            char *p=strtok(NULL,"/");
            //printf("路径是：%s\n",p);
            if(strcmp(ch,"cd")==0||strcmp(ch,"remove")==0
               ||strcmp(ch,"mkdir")==0||strcmp(ch,"download")==0){
                send(socket_fd,buf2,sizeof(buf2),0);
            }
            else if(strcmp(ch1,"ls")==0&&p==NULL){
                send(socket_fd,buf2,sizeof(buf2),0);
            }
            else if(strcmp(ch1,"pwd")==0&&p==NULL){
                send(socket_fd,buf2,sizeof(buf2),0);
            }
            //上传文件
            else if(strcmp(ch,"upload")==0){
                //文件名
                char filename[100]={0};
                strncpy(filename,p,strlen(p)-1);
                //读取文件内容
                int file_fd=open(filename,O_RDONLY);
                if(file_fd==-1){
                    printf("%s不存在\n",filename);
                    continue;
                }
                send(socket_fd,buf2,sizeof(buf2),0); 
                struct stat stat_t;
                fstat(file_fd,&stat_t);
                send(socket_fd,&stat_t.st_size,sizeof(off_t),MSG_NOSIGNAL);
                while(1){
                    bzero(buf,sizeof(buf));
                    //先读取数据
                    int res_file_num=read(file_fd,buf,sizeof(buf));
                    if(res_file_num==0){
                        //文件读完
                        int num=0;
                        send(socket_fd,&num,sizeof(int),0);
                        printf("文件上传完毕\n");
                        break;
                    }
                    //先发数据长度
                    int file_len=res_file_num;
                    send(socket_fd,&file_len,sizeof(int),0);
                    //再发数据
                    send(socket_fd,buf,sizeof(buf),0);
                }
                close(file_fd);

            }
            else if(strcmp(ch,"download")==0){
                int flag;
                recv(socket_fd,&flag,sizeof(int),MSG_WAITALL);
                if(flag==-1){
                    printf("要求的文件不存在\n");
                    exit(1);
                }
                //接收文件大小
                off_t file_size;
                recv(socket_fd,&file_size,sizeof(off_t),MSG_WAITALL);
                printf("file size:%ld\n",file_size);
                mkdir("download",0777);
                chdir("download");
                //文件存在则接收文件内容
                //char name[100]="要下载文件名";
                char name[100]={0};
                strncpy(name,p,strlen(p)-1);
                int fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0664);
                chdir("-");
                //下载服务端的文件
                while(1){
                    int buf_len;
                    char buf[1024]={0};
                    int ret_recv = recv(socket_fd, &buf_len, sizeof(int), MSG_WAITALL);
                    printf("ret_recv = %d\n", ret_recv);
                    printf("buf_len = %d\n", buf_len);
                    if(buf_len == 0){
                        printf("文件传输完毕\n");
                        break;
                    }
                    recv(socket_fd, buf, buf_len, MSG_WAITALL);
                    write(fd, buf, buf_len);
                }
            }
            else{
                char buf3[1024]={0};
                strncpy(buf3,buf2,strlen(buf2)-1);
                printf("%s未找到指令\n",buf3);
            }
        }
    }
    close(socket_fd);

    return 0;
}

